package com.tongji.diy.view.component3D
{
	import com.tongji.diy.model.WallDoor;
	import com.tongji.diy.utils.RoomParameters;
	import com.tongji.diy.view.material.DefaultMaterial;
	import com.tongji.diy.view.material.WhiteMaterial;
	
	import flare.core.Mesh3D;
	import flare.core.Surface3D;
	import flare.core.Texture3D;
	import flare.events.MouseEvent3D;
	import flare.materials.Material3D;
	import flare.materials.Shader3D;
	import flare.materials.filters.TextureFilter;
	
	import flash.geom.Point;
	import flash.geom.Vector3D;
	
	import mx.messaging.AbstractConsumer;

	public class Door extends Mesh3D
	{
		//法向量
		private var dirVec:Point = new Point(0,1);
		
		private var _wallDoor:WallDoor;
		private var _bottom:Number;
		private var _top:Number;
		private var _width:Number;
		private var _pos:Vector3D;
		private var _picSrc:String;
		private var _mat:Material3D;
		private var _wallWidth:Number;
		
		private var _bRolling:Boolean;
		private var _rollAngle:Number = -0.001;
		private var _preTime:Number = -1;
		
		//zdp
		private var _globalposition:Point = new Point(0,0);
		private var _globalangle:Number = 0;
		
		//frank 开关门标志
		private var _open:Boolean = false;
		
		public function Door(wallDoor:WallDoor,pos:Vector3D,roomBorder:RoomBorder)
		{
			this._wallDoor = wallDoor;
			this._bottom = wallDoor.bottom;
			this._top = wallDoor.top;
			this._width = wallDoor.width;
			this._pos = pos;
			this._picSrc = wallDoor.picSrc;
			this._wallWidth = RoomParameters.wallWidth+1;
			
			var tex:Texture3D = new Texture3D(_picSrc);
			var texFilter:TextureFilter = new TextureFilter(tex);
			var shader:Shader3D = new Shader3D();
			shader.filters.push(texFilter);
			this._mat = shader as Material3D;
			/*
			 *	绘制
			 */
			
			//前后
			surfaces[0] = new Surface3D("Front&Back");
			surfaces[0].material = this._mat;
			surfaces[0].addVertexData( Surface3D.POSITION );
			surfaces[0].addVertexData( Surface3D.UV0 );
			surfaces[0].addVertexData( Surface3D.NORMAL );
			//其它
			surfaces[1] = new Surface3D("Except Front&Back");
			surfaces[1].material = this._mat;
			surfaces[1].addVertexData( Surface3D.POSITION );
			surfaces[1].addVertexData( Surface3D.UV0 );
			surfaces[1].addVertexData( Surface3D.NORMAL );
			
			//0
			surfaces[0].vertexVector.push(-this._wallWidth,this._bottom,-this._width/2,
				0,0,
				0,1,1);
			//1
			surfaces[0].vertexVector.push(-this._wallWidth,this._top,-this._width/2,
				0,1,
				0,1,0);
			//2
			surfaces[0].vertexVector.push(this._wallWidth,this._top,-this._width/2,
				1,1,
				0,1,0);
			//3
			surfaces[0].vertexVector.push(this._wallWidth,this._bottom,-this._width/2,
				1,0,
				0,1,1);
			//4
			surfaces[0].vertexVector.push(-this._wallWidth,this._bottom,this._width/2,
				1,0,
				0,1,1);
			//5
			surfaces[0].vertexVector.push(-this._wallWidth,this._top,this._width/2,
				1,1,
				0,1,0);
			//6
			surfaces[0].vertexVector.push(this._wallWidth,this._top,this._width/2,
				0,1,
				0,1,0);
			//7
			surfaces[0].vertexVector.push(this._wallWidth,this._bottom,this._width/2,
				0,0,
				0,1,1);
			
			surfaces[0].indexVector.push(0,5,1, 0,4,5);
			surfaces[0].indexVector.push(2,6,3, 6,7,3);
			
			//0
			surfaces[1].vertexVector.push(-this._wallWidth,this._bottom,-this._width/2,
				0,0,
				0,1,1);
			//1
			surfaces[1].vertexVector.push(-this._wallWidth,this._top,-this._width/2,
				0,1,
				0,1,0);
			//2
			surfaces[1].vertexVector.push(this._wallWidth,this._top,-this._width/2,
				1,1,
				0,1,0);
			//3
			surfaces[1].vertexVector.push(this._wallWidth,this._bottom,-this._width/2,
				1,0,
				0,1,1);
			//4
			surfaces[1].vertexVector.push(-this._wallWidth,this._bottom,this._width/2,
				1,0,
				0,1,1);
			//5
			surfaces[1].vertexVector.push(-this._wallWidth,this._top,this._width/2,
				1,1,
				0,1,0);
			//6
			surfaces[1].vertexVector.push(this._wallWidth,this._top,this._width/2,
				0,1,
				0,1,0);
			//7
			surfaces[1].vertexVector.push(this._wallWidth,this._bottom,this._width/2,
				0,0,
				0,1,1);
			surfaces[1].indexVector.push(2,1,6, 1,5,6);
			surfaces[1].indexVector.push(2,3,1, 3,0,1);
			surfaces[1].indexVector.push(3,7,0, 0,7,4);
			surfaces[1].indexVector.push(6,5,7, 5,4,7);
			
			this.refresh(this._pos,roomBorder);
			//frank 点击房门实现开关门
//			this.addEventListener(MouseEvent3D.CLICK, clickOpenDoor);
		}
		
//		private function clickOpenDoor(e:MouseEvent3D):void
//		{
//			this.removeEventListener(MouseEvent3D.CLICK, clickOpenDoor);
//			open();
//			/*while(!_open)
//			{}
//			if(_open)*/
//				this.addEventListener(MouseEvent3D.CLICK, clickCloseDoor);
//		}
//		
//		private function clickCloseDoor(e:MouseEvent3D):void
//		{
//			this.removeEventListener(MouseEvent3D.CLICK, clickCloseDoor);
//			close();
//			/*while(!_open)
//			{}
//			if(_open)*/
//				this.addEventListener(MouseEvent3D.CLICK, clickOpenDoor);			
//		}
		
		public function open():void
		{
			if(this._rollAngle >= 90.0)
				return;
			
			var date:Date = new Date();
			var time:Number = date.time;
			if(this._preTime == -1)
				this._preTime = time;
			
			var deltaAngle:Number = (time - this._preTime)/100*9;
			this._rollAngle += deltaAngle;
			this._preTime = time;
			
			if(this._rollAngle > 90.0)
			{
				deltaAngle -= this._rollAngle - 90.0;
				this._rollAngle = 90.000001;
				this._preTime = -1;
			}
			
			this.translateZ(this._width/2);
			this.rotateY(deltaAngle);
			this.translateZ(-this._width/2);
			trace("open");
		}
		
		public function close():void
		{
			if(this._rollAngle <= 0.0)
				return;
			
			var date:Date = new Date();
			var time:Number = date.time;
			if(this._preTime == -1)
				this._preTime = time;
			
			var deltaAngle:Number = -(time - this._preTime)/100*9;
			this._rollAngle += deltaAngle;
			this._preTime = time;
			
			if(this._rollAngle < 0.0)
			{
				deltaAngle -= 0 - this._rollAngle;
				this._rollAngle = -0.000001;
				this._preTime = -1;
			}
			
			this.translateZ(this._width/2);
			this.rotateY(deltaAngle);
			this.translateZ(-this._width/2);
			trace("close");
		}
		
		public function refresh(pos:Vector3D,roomBorder:RoomBorder):void
		{
			this.setPos(pos,roomBorder);
			this.setRot(roomBorder.getNormal());
		}
		
		public function get bottom():Number
		{
			return this._bottom;
		}
		
		public function get top():Number
		{
			return this._top;
		}
		
		public function get width():Number
		{
			return this._width;
		}
		
		public function get wallDoor():WallDoor
		{
			return this._wallDoor;
		}
		public function get globalpos():Point
		{
			//this.refreshonly(this._pos,this._roomborder);
			return this._globalposition;
		}
		public function get globalangle():Number
		{
			//this.refreshonly(this._pos,this._roomborder);
			return this._globalangle;
		}
		//将位置调整到附着墙的内外墙中线上
		public function setPos(pos:Vector3D, roomBorder:RoomBorder):void
		{
			var start:Point = new Point(roomBorder.start.data.globalX, roomBorder.start.data.globalY);
			var end:Point = new Point(roomBorder.end.data.globalX, roomBorder.end.data.globalY);
			var newPos:Vector3D = new Vector3D();
			var k:Number = 0;
			var b:Number = 0;
			
			if( (end.x - start.x) != 0)
			{
				k = (end.y - start.y)/(end.x - start.x);
				b = start.y - k*start.x;
				
				newPos.x = (pos.x+(pos.z-b)*k)/(1+k*k);
				newPos.z = newPos.x*k+b;
				newPos.y = 0;
				this._globalposition.x = (pos.x+(pos.z-b)*k)/(1+k*k);
				this._globalposition.y = newPos.x*k+b;
			}
			else
			{
				newPos.x = start.x;
				newPos.z = pos.z;
				newPos.y = 0;
				this._globalposition.x = start.x;
				this._globalposition.y = pos.z;
			}
			
			this.setPosition(newPos.x, newPos.y, newPos.z, 1, false);
		}
		
		//normal是单位化的Point
		public function setRot(normal:Point):void
		{
			var angle:Number = Math.acos((dirVec.x*normal.x + dirVec.y*normal.y)/(normal.length*dirVec.length))*180/Math.PI;
			
			//单位化normal
			normal.x /= normal.length;
			normal.y /= normal.length;
			
			if(dirVec.x > normal.x)
			{
				this.setRotation(0, -angle+90, 0);
				this._globalangle = -angle+90;
			}
			else
			{
				this.setRotation(0, angle+90, 0);
				this._globalangle = angle+90;
			}
		}
	}
}